//
//  YXMessageViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/21.
//  Copyright © 2017年 杨旭. All rights reserved.
//
#define kLeftTableView 500
#define kRightTableView 600
#import "YXMessageViewController.h"
#import "YXMessageModel.h"
#import "YXWebViewController.h"
#import "YXCommentsCell.h"
@interface YXMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *leftTableView;
@property (nonatomic ,strong) UITableView *rightTableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@end

@implementation YXMessageViewController

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];
    self.title = @"消息列表";
    [self requestDataCommentsList];
    [self requestDataNoticeList];
    [self createMenuUI];
    [self createLeftTableViewUI];
}

- (void)requestDataCommentsList {

    
}

- (void)requestDataNoticeList{
    
//    http://121.196.210.134:8082/test_mpos/wx/getNewNoticeList.do
    NSString *urlStr = @"getNewNoticeList.do";
    NSDictionary *parameter = @{@"sessionId":[YXUserInfoManager getUserInfo].sessionId};
    [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if (REQUESTDATASUCCESS) {
            self.dataArr = [YXMessageModel mj_objectArrayWithKeyValuesArray:responseObject[@"body"]];
            [self.rightTableView reloadData];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)createMenuUI {
    
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 60))];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    
    NSArray *titleArr = @[@"评论",@"通知"];
    UISegmentedControl * segment = [[UISegmentedControl alloc] initWithItems:titleArr];
    segment.frame = CGRectMake((kScreenWidth - 220)/2, 15, 220, 30);
    segment.selectedSegmentIndex = 0;
    segment.tintColor = color_lineColor;
    //    选中的颜色
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:color_globalColor} forState:UIControlStateSelected];
    //    未选中的颜色
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    [segment addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:(UIControlEventValueChanged)];
    [headerView addSubview:segment];
}

#pragma mark - 点击顶部菜单
- (void)didClicksegmentedControlAction:(UISegmentedControl *)sender {
    
    NSInteger index = sender.selectedSegmentIndex;
    NSLog(@"Index %ld", index);
    if (index == 0) {
        [self createLeftTableViewUI];

    }else {
        [self createRightTableViewUI];
    }
}


- (void)createLeftTableViewUI {
    
     // 评论
    self.leftTableView =[[UITableView alloc] initWithFrame:(CGRectMake(0, 60, kScreenWidth, kScreenHeight-60-64))];
    self.leftTableView.backgroundColor = [UIColor whiteColor];
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.tag = kLeftTableView;
    self.leftTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.leftTableView];
    
    // 评论cell
    [self.leftTableView registerNib:[UINib nibWithNibName:@"YXCommentsCell" bundle:nil] forCellReuseIdentifier:@"YXCommentsCell"];
    
    // 注册个人消费cell
    [self.leftTableView registerNib:[UINib nibWithNibName:@"ReconciliationCell" bundle:nil] forCellReuseIdentifier:@"ReconciliationCell"];
}
- (void)createRightTableViewUI {
    
    // 通知
    self.rightTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 60, kScreenWidth, kScreenHeight-60 -64))];
    self.rightTableView.backgroundColor = [UIColor whiteColor];
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.tag = kRightTableView;
    self.rightTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.rightTableView];
    
    [self.rightTableView registerNib:[UINib nibWithNibName:@"YXCommentsCell" bundle:nil] forCellReuseIdentifier:@"YXCommentsCell"];
    
}


#pragma mark - UITableView的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView.tag == kLeftTableView) {
        return 3;
    }else{
        return self.dataArr.count;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == kLeftTableView) {
        YXCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXCommentsCell" forIndexPath:indexPath];
        return cell;
    }else {
        YXCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXCommentsCell" forIndexPath:indexPath];
        cell.model = self.dataArr[indexPath.row];
        return cell;
//        static NSString *cellID = @"noticeCellID";
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//        if (!cell) {
//            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellID];
//            cell.textLabel.font = [UIFont systemFontOfSize:16];
//            cell.textLabel.textColor = color_textOne;
//        }
//        YXMessageModel *model = self.dataArr[indexPath.row];
//        cell.textLabel.text = model.title;
//        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"logo"]];
//        cell.detailTextLabel.text = model.content;
//        cell.selectionStyle =UITableViewCellSelectionStyleNone;
//        return cell;
    }
    
    
    return [[UITableViewCell alloc] init];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == kLeftTableView) {
        
    }else{
        YXWebViewController *webView = [[YXWebViewController alloc] init];
        YXMessageModel *model = self.dataArr[indexPath.row];
        webView.ID = model.ID;
        webView.title = @"通知";
        [self.navigationController pushViewController:webView animated:YES];
    }
}

@end
